﻿using System;
using System.IO;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.Diagnostics;
using System.Drawing.Imaging;
using thermocam;
using System.Runtime.InteropServices;
using System.Net;
using System.Media;

namespace thermocam
{
    
    public partial class main : Form
    {
       
        
        public main()
        {
            InitializeComponent();
            
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            axLVCam1.DoCameraAction(6);
            //String win1 = "Test Window"; //The name of the window
            //CvInvoke.cvNamedWindow(win1); //Create the window using the specific name

            //Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0)); //Create an image of 400x200 of Blue color
            //MCvFont f = new MCvFont(FONT.CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0); //Create the font

            //img.Draw("Hello, world", ref f, new System.Drawing.Point(10, 80), new Bgr(0, 255, 0)); //Draw "Hello, world." on the image using the specific font
            //short [][]img = (short[][])axLVCam1.GetImage(0);//open the images in the directory "Debug"
            //ToBitmap()将IImage格式转换为Bitmap格式，便能为PictureBox所用了。
            //Image<Bgr, byte> img1 = (Image<Bgr, byte>)img;
           // pictureBox1.Image = img1.ToBitmap();
            //pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

            //CvInvoke.cvShowImage(win1, img); //Show the image
            //CvInvoke.cvWaitKey(0);  //Wait for the key pressing event
            //CvInvoke.cvDestroyWindow(); //Destory the window
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            startCamera();
            //axLVCam1.DoCameraAction(0);
            
        }
        

        
        
        

        private void functionToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "(*.jpg;*.bmp;*.png)|*.jpg;*.bmp;*.png";//文件类型过滤
            openFileDialog1.InitialDirectory = "C:\\";//打开对话框默认路径
            openFileDialog1.FileName = "";//清空默认openFileDialog1
            openFileDialog1.Multiselect = false;//多选时设为true
            openFileDialog1.ShowDialog();//显示打开对话框
            if (openFileDialog1.FileName != "")//不为空时路径填入文本框
            {
                Image<Bgr, byte> img = new Image<Bgr, byte>(openFileDialog1.FileName);//open the images in the directory "Debug"
                //ToBitmap()将IImage格式转换为Bitmap格式，便能为PictureBox所用了。
                pictureBox2.Image = img.ToBitmap();
                pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                //textBox1.Text = openFileDialog1.FileName;
            }
        }

        private void cameraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectTab(0);
        }

        private void analyseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectTab(1);
        }
        private void startCamera()
        {
            short original = (short)axLVCam1.GetImage(0);
            MessageBox.Show(original.ToString(), "OK");
          //  Bitmap newBitmap = new Bitmap(320,200);

           // for (int j = 0; j < 200; j++)
            //{
               // for (int i = 0; i < 320; i++)
               // {
               //     byte[] b = System.BitConverter.GetBytes(original[i, j]);
              //      Color c = Color.FromArgb(b[3], b[2], b[1]);
//
               //     newBitmap.SetPixel(i, j, c);
             //   }

            //}


            //pictureBox1.Image = newBitmap;
            
        }
        
        
        

        private void button4_Click(object sender, EventArgs e)
        {
            short connectStatus;
            if ((short)axLVCam1.GetCameraProperty(69) == 1)
            {
                connectStatus = axLVCam1.Connect(11, 0, 6, 2, "192.168.1.100");
                if (connectStatus != 0)
                {
                    MessageBox.Show(axLVCam1.GetError(connectStatus), "OK");
                }
                else
                {
                    button4.Text = "Disconnect";
                    MessageBox.Show("Successfully connected!", "OK");
                    axLVCam1.SetCameraProperty(66, 160);
                    axLVCam1.SetCameraProperty(67, 120);
                }
                
                
            }
            else
            {
                connectStatus = axLVCam1.Disconnect();
                
                button4.Text = "Connect";
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            axLVCam1.DoCameraAction(7);
        }

        private void button6_Click(object sender, EventArgs e)
        {

            
        }

        
        

        

        
    }
}
